package com.wanshi.filter;

import com.wanshi.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = "/ss/*",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class QuanXianFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user!=null){
            chain.doFilter(req, resp);
        }else {
            req.setAttribute("error","你还没有登录，请先登录");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
